package com.yanqu.road.web.controller.game;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.web.HttpJsonWebParam;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.server.helper.SignHelper;
import com.yanqu.road.server.manger.AppKeyMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.web.WebServer;
import com.yanqu.road.web.controller.BaseJsonHttpServlet;
import com.yanqu.road.webservice.invoke.GameWebServiceInvoke;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/executeAdminOperate")
public class ExecuteAdminOperate extends BaseJsonHttpServlet {

	private static Logger logger = LogManager.getLogger(ExecuteAdminOperate.class.getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		logger.info("request executeAdminOperate");
		HttpJsonWebParam httpJsonWebParam = super.parseRequest(request);
		JSONObject paramObj = httpJsonWebParam.getParamObj();
		String openId;
		int treatType;    //1.禁言2.解禁言 3.踢下线.4.禁登录 5.解登录
		long treatTime;
		long time;
		long userId;
		String nickName;
		int serverId;
		String sign;
		try{
			openId = paramObj.getString("userID");
			treatType = paramObj.getIntValue("treatType");    //1.禁言2.解禁言 3.踢下线.4.禁登录 5.解登录
			treatTime = paramObj.getIntValue("treatTime");
			time = System.currentTimeMillis() + treatTime * 60000;
			userId = paramObj.getLong("actorID");
			nickName = paramObj.getString("actorName");
			sign = paramObj.getString("sign");
		}catch (Exception e){
			logger.error("executeAdminOperate argument error:" + e);
			sendResult(response, 0, "argument error");
			return;
		}
		try{
			String content = AppKeyMgr.getAndroidAppSecret() + openId + paramObj.getIntValue("serverID") + paramObj.getIntValue("atime") + treatType;
			if(!SignHelper.checkSign(sign, content)){
				if(ServerListMgr.isCnMergeServer(WebServer.getInstance().getServerId())){
					content = ChannelConfig.APP_SECRET_CONFIG + openId + paramObj.getIntValue("serverID") + paramObj.getIntValue("atime") + treatType;
					if(!SignHelper.checkSign(sign, content)){
						logger.error("executeAdminOperate sign校验不通过");
						sendResult(response, 0, "sign error");
						return;
					}
				}else {
					logger.error("executeAdminOperate sign校验不通过");
					sendResult(response, 0, "sign error");
					return;
				}
			}

			JSONObject param = new JSONObject();
			param.put("userID", userId + "");
			if (1 == treatType || 2 == treatType) {//禁言、解禁
				param.put("forbiddenSpeakTime", time);
				String jsonStr = GameWebServiceInvoke.executeAdminOperate("AgentForbid|forbidPlayerSpeak", param.toString());
				logger.info("executeAdminOperate AgentForbid|forbidPlayerSpeak=={}", jsonStr);
				sendResult(response, 1, "success");
			} else if (3 == treatType) {//踢下线
				String jsonStr = GameWebServiceInvoke.executeAdminOperate("AgentPlayer|kickOutPlayer", param.toString());
				logger.info("AgentPlayer|kickOutPlayer=={}", jsonStr);
				sendResult(response, 1, "success");
			} else if (4 == treatType || 5 == treatType) {//禁止登录、解禁
				param.put("blackForbiddenTime", time);
				String jsonStr = GameWebServiceInvoke.executeAdminOperate("AgentForbid|forbidPlayer", param.toString());
				logger.info("executeAdminOperate AgentForbid|forbidPlayer=={}", jsonStr);
				sendResult(response, 1, "success");
			} else {
				sendResult(response, 0, "treatType error");
			}
		} catch (Exception e) {
			logger.error("executeAdminOperate error:" + e);
			sendResult(response, 0, "other error");
		}
	}
}
